(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  window.Point = (function() {
    function Point(map, elem) {
      this.map = map;
      this.elem = j$(elem);
      this.name = j$.trim(this.elem.text());
      
      // add self to parent map
      this.map.points.push(this);
      
      // read attributes from the dom
      this.color = this.elem.attr('data-color') || 'light_blue';
      this.lat = Number(this.elem.attr('data-lat'));
      this.lng = Number(this.elem.attr('data-lng'));
      this.regionLat = Number(this.elem.attr('data-region-lat') || this.lat);
      this.regionLng = Number(this.elem.attr('data-region-lng') || this.lng);
      this.regionString = this.elem.attr('data-region');
      this.address = this.elem.attr('data-address');
      this.subtitle = this.elem.attr('data-subtitle');
      this.cityStateZip = this.elem.attr('data-city-state-zip');
      this.phone = this.elem.attr('data-phone');
      this.fax = this.elem.attr('data-fax');
      this.image = this.elem.attr('data-image');
      this.direx = this.elem.attr('data-direx');
      
      this.link = this.elem.attr('href');
      this.createImage();
      this.determineRegion();
      this.bindHover();
      this.determineTooltipPosition();
      this.bindDialog();
      
      // pass click on to parent region from world view
      var self = this;
      this.img.bind('click', function(){
        self.region.elem.trigger('click');
      });
    }
    Point.prototype.createImage = function() {
//      this.imageSrc = "/images2011/maps/" + this.color + "_point.png";
    	this.imageSrc = contextPath + "/resources/portal/img/maps/" + this.color + "_point.png";
      this.img = j$("<img src='" + this.imageSrc + "' class='point' />");
      return this.img.appendTo('#map');
    };
    Point.prototype.draw = function() {
      var ar, x, y, zd, size;
      x = this.lng2x();
      y = this.lat2y();
      ar = this.map.activeRegion;
      if (ar && (zd = ar.zoomable.dimensions)) {
        x = ((x - zd.leftNum) / zd.widthNum) * this.map.widthNum;
        y = ((y - zd.topNum) / zd.heightNum) * this.map.heightNum;
      }
      this.hide();
      
      size = ar ? '8px' : '5px';
      
      this.img.css({
        top: y,
        left: x,
        width: size,
        height: size
      });
      if (!ar || ar === this.region) {
        return this.show();
      }
    };
    Point.prototype.hide = function() {
      return this.img.hide();
    };
    Point.prototype.show = function() {
      return this.img.show();
    };
    Point.prototype.lng2x = function(forRegion) {
      var lng, xfactor, xoffset;
      if (forRegion == null) {
        forRegion = false;
      }
      xfactor = this.map.xfactor;
      xoffset = this.map.xoffset;
      lng = this.lng;
      if (forRegion || (this.region && this.map.activeRegion)) {
        xoffset = this.region.xoffset;
        xfactor = this.region.xfactor;
        lng = this.regionLng;
      }
      return (lng * xfactor) + xoffset;
    };
    Point.prototype.lat2y = function(forRegion) {
      var lat, yfactor, yoffset;
      if (forRegion == null) {
        forRegion = false;
      }
      yfactor = this.map.yfactor;
      yoffset = this.map.yoffset;
      lat = this.lat;
      if (forRegion || (this.region && this.map.activeRegion)) {
        yoffset = this.region.yoffset;
        yfactor = this.region.yfactor;
        lat = this.regionLat;
      }
      return (lat * yfactor) + yoffset;
    };
    Point.prototype.determineRegion = function() {
      var bottom, left, name, region, right, top, x, y, zd, _ref;
      _ref = this.map.regions;
      for (name in _ref) {
        region = _ref[name];
        zd = region.zoomable.dimensions;
        top = zd.topNum;
        left = zd.leftNum;
        right = left + zd.widthNum;
        bottom = top + zd.heightNum;
        x = this.lng2x();
        y = this.lat2y();
        if ((left < x && x < right) && (top < y && y < bottom)) {
          this.region = region;
        }
      }
      if (this.regionString) {
        return this.region = map.regions[this.regionString];
      }
    };
    Point.prototype.bindDialog = function() {
      return this.img.qtip({
        api: {
          beforeContentUpdate: __bind(function(content) {
            return this.map.dialog.render(this);
          }, this),
          beforeShow: __bind(function() {
            if (!this.map.activeRegion) return false;
            this.map.lastPinClicked = this;
            if (this.map.activeRegion || !this.region) {
              return true;
            }
            if (this.region) {
              this.region.elem.trigger('click', this);
            }
            return false;
          }, this),
          onShow: __bind(function() {
            this.map.dialogOpen = true;
            this.focus();
          }, this),
          onHide: __bind(function() {
            this.map.dialogOpen = false;
            this.map.blurPoints();
          }, this)
        },
        hide: {
          fixed: true,
          when: {
            event: 'mouseout'
          }
        },
        show: {
          solo: true,
          when: {
            event: 'mouseover'
          }
        },
        position: {
          corner: {
            target: this.tooltipPos,
            tooltip: this.tooltipPos
          }
        },
        style: {
          tip: {
            corner: this.tooltipPos,
            color: '#eee',
            size: {
              x: 16,
              y: 12
            }
          },
          border: {
            color: '#eee',
            width: 3
          },
          name: 'light',
          width: 241,
          padding :'0px 0px'
        }
      });
    };
    Point.prototype.determineTooltipPosition = function() {
      var x, y, zd;
      this.tooltipPos = this.elem.attr('data-tooltip-pos');
      if (!this.tooltipPos) {
        this.tooltipPos = 'leftMiddle';
        if (this.region) {
          x = this.lng2x('forRegion');
          y = this.lat2y('forRegion');
          if (zd = this.region.zoomable.dimensions) {
            x = ((x - zd.leftNum) / zd.widthNum) * this.map.widthNum;
            y = ((y - zd.topNum) / zd.heightNum) * this.map.heightNum;
          }
          if (x < (this.map.widthNum / 2)) {
            this.tooltipPos = 'leftMiddle';
          } else {
            this.tooltipPos = 'rightMiddle';
          }
          if (y > (this.map.heightNum * 0.70)) {
            return this.tooltipPos = 'bottomMiddle';
          } else if (y < (this.map.heightNum * 0.30)) {
            return this.tooltipPos = 'topMiddle';
          }
        }
      }
    };
    Point.prototype.focus = function() {
      if ( !this.map.activeRegion ) { return; }
//      this.img.attr('src', '/images2011/maps/point_hover.png');
      this.img.attr('src',contextPath+'/resources/portal/img/maps/point_hover.png');
      return this.elem.addClass('active');
    };
    Point.prototype.blur = function() {
      if (!(this.map.dialogOpen)) {
        this.img.attr('src', this.imageSrc);
        return this.elem.removeClass('active');
      }
    };
    Point.prototype.bindHover = function() {
      this.img.bind('mouseover', __bind(function(e) {
        this.focus();
        if (this.region && !this.map.activeRegion) {
          return this.region.elem.trigger('mousemove', e);
        }
      }, this));
      return this.img.bind('mouseout', __bind(function(e) {
        this.map.blurPoints();
        if (this.region && !this.map.activeRegion) {
          return this.region.elem.trigger('mouseout', e);
        }
      }, this));
    };
    return Point;
  })();
}).call(this);
